package com.ruoyi.web.controller.mobile;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.mobile.domain.User;
import com.ruoyi.mobile.domain.UserInfo;
import com.ruoyi.mobile.mapper.UserMapper;
import com.ruoyi.mobile.model.request.QuestionaireRequest;
import com.ruoyi.mobile.model.request.UserQuestionnaireRequest;
import com.ruoyi.mobile.model.vo.QuestionContentVo;
import com.ruoyi.mobile.model.vo.QuestionaireVo;
import com.ruoyi.mobile.model.vo.UserQuestionnaireVo;
import com.ruoyi.mobile.service.IUserInfoService;
import com.ruoyi.mobile.service.IUserInfoTypeService;
import com.ruoyi.mobile.util.ExcelQuestionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/mobile/userquestionaire")
public class UserQuestionaireController  extends BaseController {

    @Autowired
    public IUserInfoService userInfoService;

    @Autowired
    public IUserInfoTypeService userInfoTypeService;

    @Autowired
    public UserMapper userMapper;

    @ResponseBody
    @RequestMapping("/list")
    @PreAuthorize("@ss.hasPermi('mobile:userquestionaire:list')")
    public TableDataInfo list(QuestionaireRequest questionaireRequest) {
        User user = userMapper.selectUserById(questionaireRequest.getUserId());
        startPage();
        List<QuestionaireVo> list = userInfoService.selectListByUserId(questionaireRequest);
        for (QuestionaireVo questionaireVo : list) {
            questionaireVo.setNickname(user.getNickname());
            questionaireVo.setPhone(user.getPhone());
        }
        return getDataTable(list);
    }


    @GetMapping("/detail")
    @ResponseBody
    public AjaxResult detail(@RequestParam(name = "userId") Integer userId, @RequestParam(name = "type") Integer type, @RequestParam(name = "subType", defaultValue = "0") Integer subType) throws JsonProcessingException {
        UserInfo userInfo = userInfoService.selectByUserIdAndSubType(userId, type, subType);
        if (userInfo == null) {
            return AjaxResult.success();
        }
        UserQuestionnaireVo userQuestionnaireVo = new UserQuestionnaireVo();
        userQuestionnaireVo.setId(userInfo.getId());
        userQuestionnaireVo.setType(userInfo.getType());
        userQuestionnaireVo.setSubType(userInfo.getSubType());

        if (userInfo.getContent() != null && !"".equals(userInfo.getContent())) {
            ObjectMapper mapper = new ObjectMapper();
            mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
            Map<String, Object> map = mapper.readValue(userInfo.getContent(), Map.class);
            if (map != null) {
                userQuestionnaireVo.setData(map);
            }
        }

        userQuestionnaireVo.setCreateTime(userInfo.getCreateTime());
        userQuestionnaireVo.setEditable(1);

        return AjaxResult.success(userQuestionnaireVo);
    }


    @PostMapping("/add")
    @CrossOrigin
    @ResponseBody
    @PreAuthorize("@ss.hasPermi('mobile:userquestionaire:add')")
    @Log(title = "用户调查问卷", businessType = BusinessType.INSERT)
    public AjaxResult add(@RequestBody UserQuestionnaireRequest userQuestionnaireRequest) throws JsonProcessingException {
        userInfoService.insertUserInfo(userQuestionnaireRequest.getUserId(), userQuestionnaireRequest);
        return AjaxResult.success();
    }


    @PostMapping("/update/{id}")
    @CrossOrigin
    @ResponseBody
    @PreAuthorize("@ss.hasPermi('mobile:userquestionaire:edit')")
    @Log(title = "用户调查问卷", businessType = BusinessType.UPDATE)
    public AjaxResult update(@PathVariable("id") int id , @RequestBody UserQuestionnaireRequest userQuestionnaireRequest) throws JsonProcessingException {
        userInfoService.updateByUserIdAndType(userQuestionnaireRequest.getUserId(), id, userQuestionnaireRequest);
        return AjaxResult.success();
    }

    @PreAuthorize("@ss.hasPermi('mobile:userquestionaire:remove')")
    @Log(title = "用户调查问卷", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ResponseBody
    public AjaxResult remove(@PathVariable Integer[] ids, @RequestParam(name="userId") Integer userId)
    {
        return toAjax(userInfoService.deleteByIds(ids, userId));
    }

    @PostMapping("/export")
    public void exportQuestion(HttpServletResponse response, Integer type) {
        List<QuestionContentVo> list = userInfoService.selectContentsByType(type);
        ExcelQuestionUtil util = new ExcelQuestionUtil();
        util.exportExcel(response, type, list);
    }
}
